package com.hly.tool.log;

import matrix.db.Context;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.*;

/**
 * Created with IntelliJ IDEA.
 * Copyright@ Apache Open Source Organization
 *
 * @Auther: HLY
 * @Date: 2023-05-30-16:17
 * @Description:
 */
public class LoggerUti
{
    //加锁
    private static ReentrantLock lock = new ReentrantLock();

    //格式化时间

    
    public String testLog(Context context,String[] args){
        Logger logger = getLogger(this.getClass());
        logger.info(" -------------------- info -------------------- ");
        logger.info(" -------------------- info -------------------- ");
        logger.info(" -------------------- info -------------------- ");
        return "";
    }

    public Logger getLogger(Class clazz)
    {
        try
        {
            lock.lock();
            Logger _logger = Logger.getLogger(clazz.getSimpleName());
            _logger.setUseParentHandlers(false);
            ConsoleHandler consoleHandler = new ConsoleHandler();
            consoleHandler.setFormatter(new MyFormatter());
            _logger.addHandler(consoleHandler);
            return _logger;
        } finally
        {
            lock.unlock();
        }
    }


    static  class MyFormatter extends Formatter
    {
        private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        @Override
        public String format(LogRecord arg0)
        {

            StackTraceElement stackTrace = Thread.currentThread().getStackTrace()[8];

            // TODO Auto-generated method stub
            //创建StringBuilder对象来存放后续需要打印的日志内容
            StringBuilder builder = new StringBuilder(4000);

            String dateStr = sdf.format(new Date());

            builder.append(dateStr);
            builder.append(" - ");

            //拼接日志级别
            builder.append(arg0.getLevel()).append(" - ");

            //拼接方法名带行号
//            builder.append(arg0.getSourceMethodName()).append(":"+Thread.currentThread().getStackTrace()[8].getLineNumber()+"").append(" - ");
            //添加文件名
            builder.append(stackTrace.getFileName()+".");

            //添加方法名
            builder.append(arg0.getSourceMethodName());

            //添加行号
            builder.append(":"+Thread.currentThread().getStackTrace()[8].getLineNumber()+"");
            //添加分隔符
            builder.append(" - ");

            //拼接日志内容
            builder.append(arg0.getMessage());
            //日志换行
            builder.append("\r\n");

            return builder.toString();
        }

        @Override
        public String getHead(Handler h)
        {
            // TODO Auto-generated method stub
//            return "此处为日志的头部信息\r\n";
            return "";
        }

        @Override
        public String getTail(Handler h)
        {
            // TODO Auto-generated method stub
//            return "此处为日志的尾部信息\r\n";
            return "\r\n";
        }

    }

}
